/**
 * 用户管理
 */
const db = require("../database/db-mysql")
const userService = require("../service/users")

module.exports = {
    getUserList: async function (ctx, next) {
        let { username } = ctx.request.query;
        let params = { username }
        for (var key in params) {
            if (!params[key]) {
                delete params[key];
            }
        }
        console.log(params)
        let userList = await userService.getUserList(params)
        ctx.body = {
            status: 200,
            data: userList
        }
    },
    del: async function (ctx, next) {
        let { id } = ctx.request.body;
        let params = { id };
        await userService.del(params)
        ctx.body = {
            status: 200,
            message: "delete success"
        }
        // console.log("删除用户：", id)
        // await db.query("delete from user where id = ?", [id]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "delete success"
        //     }
        // })
    },
    add: async (ctx, next) => {
        let { username, password, nickname, roleId } = ctx.request.body;
        console.log("新增用户：", username, password, nickname, roleId)
        let params = { username, password, nickname, roleId };
        await userService.add(params)
        ctx.body = {
            status: 200,
            message: "delete success"
        }
        // await db.query("insert into user(username, password, nickname, role) values(?,?,?,?)", [username, password, nickname, roleId]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "add success"
        //     }
        // }).catch(err => {
        //     ctx.body = {
        //         status: 500,
        //         data: err
        //     }
        // })
    },
    update: async (ctx, next) => {
        let { username, password, nickname, role, id } = ctx.request.body;
        let params = { username, password, nickname, role, id };
        await userService.update(params)
        ctx.body = {
            status: 200,
            message: "修改成功"
        }
        // await db.query("update user set username =?, password=?, nickname=?, role=? where id = ?", [username, password, nickname, roleId, id]).then(res => {
        //     ctx.body = {
        //         status: 200,
        //         message: "edit success"
        //     }
        // }).catch(err => {
        //     ctx.body = {
        //         status: 500,
        //         data: err
        //     }
        // })
    }
}